﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tecnorama.MobilSatis
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        LinqDataContext db = new LinqDataContext();
        private void Form2_Load(object sender, EventArgs e)
        {
            foreach (Label item in panel1.Controls)
            {
                item.Text = char.ConvertFromUtf32(9679);
            }
            listreset();
        }

        private void listBox1_Click(object sender, EventArgs e)
        {
            try
            {
                string secilenkategori = listBox1.SelectedItem.ToString();
                if (secilenkategori == "")
                {
                    return;
                }
                listBox1.Items.Clear();
                switch (listBox1.Tag.ToString())
                {
                    case "Kategoriler":
                        var soru = (from y in db.Kategorilers where y.KategoriAdi == secilenkategori select new { y.KategoriID }).FirstOrDefault();
                        var sorgu = from x in db.Uruns where x.KategoriId == soru.KategoriID select x;
                        string eski = "";
                        foreach (var item in sorgu)
                        {
                            if (item.Markalar.MarkaFirmaAdi != eski)
                            {
                                listBox1.Items.Add(item.Markalar.MarkaFirmaAdi);
                            }
                            eski = item.Markalar.MarkaFirmaAdi;
                        }
                        listBox1.Tag = "Markalar";
                        break;
                    case "Markalar":
                        var soru1 = (from y in db.Uruns where y.Markalar.MarkaFirmaAdi == secilenkategori select new { y.UrunAdi }).FirstOrDefault();
                        var sorgu1 = from x in db.Uruns where x.Markalar.MarkaFirmaAdi == secilenkategori select x;
                        foreach (var item in sorgu1)
                        {
                            listBox1.Items.Add(item.UrunAdi);


                        }
                        listBox1.Tag = "Ürünler";
                        break;
                    case "Ürünler":
                        var soru2 = (from x in db.UrunViews where x.UrunAdi == secilenkategori select x).FirstOrDefault();
                        urungetir(soru2, "1");
                        listreset();
                        break;
                    default:
                        break;
                }
                groupBox2.Text = listBox1.Tag.ToString();

            }
            catch (Exception)
            {

                listreset();
            }


        }

        private bool urungetir(UrunView soru2, string adet)
        {
            bool geldi = true;
            if (soru2 != null)
            {
                l1.Text = soru2.UrunBarkod;
                l2.Text = soru2.KategoriAdi;
                lblmarka.Text = soru2.M_MarkaFirmaAdi;
                lblmodel.Text = soru2.UrunAdi;
                l5.Text = soru2.UretimYili.ToString();
                l6.Text = soru2.UrunStok.ToString();
                lblfiyat.Text = soru2.UrunFiyat;
                txtadet.Text = adet;
                geldi = false;
            }
            return geldi;
        }

        private void listreset()
        {
            listBox1.Items.Clear();
            listBox1.Tag = "Kategoriler";
            groupBox2.Text = "Kategoriler";
            var sorgu3 = from x in db.Kategorilers select x;
            foreach (var item in sorgu3)
            {
                listBox1.Items.Add(item.KategoriAdi);
            }
            Primhesapla();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var sor = (from x in db.UrunViews where x.UrunBarkod == textBox1.Text select x).FirstOrDefault();
            klavye1.Enabled = urungetir(sor, "1");



        }

        private void textBox1_Click(object sender, EventArgs e)
        {
            secilitext = sender as TextBox;
            klavye1.Enabled = true;
        }

        private void klavye1_Tuslandi(object sender, SanalKlavye.ButonaBasildi e)
        {
            secilitext.Text += klavye1.harf;
            if (klavye1.harf == "@")
            {
                if (secilitext.Text.Length > 0)
                {
                    secilitext.Text = secilitext.Text.Substring(0, secilitext.Text.Length - 2);

                }
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        TextBox secilitext;
        private void textBox2_Click(object sender, EventArgs e)
        {
            secilitext = sender as TextBox;
            klavye1.Enabled = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            btnsiparis.Text = "Sipariş Ekle";
            klavye1.Enabled = false;
            textBox1.Text = "";
            txtadet.Text = "";
            listreset();
            flowLayoutPanel1.Controls.Remove(buton);
        }

        private void btnsiparis_Click(object sender, EventArgs e)
        {
            try
            {
                string bilgi = "";
                int fiyat = 0;
                switch (btnsiparis.Text)
                {
                    case "Sipariş Ekle":
                        Button btn = new Button();
                        foreach (Control item in flowLayoutPanel1.Controls)
                        {
                            string a = item.Tag.ToString().Split('/')[0];
                            if (a == l1.Text)
                            {
                                btn = item as Button;
                                txtadet.Text = (int.Parse(txtadet.Text) + int.Parse(item.Tag.ToString().Split('/')[1])).ToString();
                            }
                        }
                        btn.Height = 50;
                        btn.Width = flowLayoutPanel1.Width - 4;
                        fiyat = int.Parse(txtadet.Text) * Convert.ToInt32(lblfiyat.Text);
                        btn.Text = lblmarka.Text + " " + lblmodel.Text + "\n" + txtadet.Text + " Adet---> " + fiyat.ToString() + " TL";
                        bilgi = l1.Text + "/" + txtadet.Text + "/" + fiyat.ToString();
                        btn.Tag = bilgi;
                        btn.Click += new EventHandler(btn_Click);
                        flowLayoutPanel1.Controls.Add(btn);
                        klavye1.Enabled = false;
                        textBox1.Text = "";
                        txtadet.Text = "";
                        listreset();


                        break;
                    case "Sipariş Güncelle":
                        btnsiparis.Text = "Sipariş Ekle";
                        fiyat = int.Parse(txtadet.Text) * Convert.ToInt32(lblfiyat.Text);
                        buton.Text = lblmarka.Text + " " + lblmodel.Text + "\n" + txtadet.Text + " Adet---> " + fiyat.ToString() + " TL";
                        bilgi = l1.Text + "/" + txtadet.Text + "/" + fiyat.ToString();
                        buton.Tag = bilgi;
                        klavye1.Enabled = false;
                        textBox1.Text = "";
                        txtadet.Text = "";
                        listreset();

                        break;
                    default:
                        break;
                }
                int total = 0;
                foreach (Control item in flowLayoutPanel1.Controls)
                {
                    if (item is Button)
                    {
                        total += Convert.ToInt32(item.Tag.ToString().Split('/')[2]);

                    }
                }
                btntotal.Text = "Toplam Fiyat=" + total.ToString() + " TL";

            }
            catch (Exception)
            {
                MessageBox.Show("Lütfen Adet Giriniz");
            }
        }
        Button buton;
        void btn_Click(object sender, EventArgs e)
        {
            btnsiparis.Text = "Sipariş Güncelle";
            buton = sender as Button;
            var soru = (from x in db.UrunViews where x.UrunBarkod == buton.Tag.ToString().Split('/')[0] select x).FirstOrDefault();
            urungetir(soru, buton.Tag.ToString().Split('/')[1]);
            timer1.Start();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {

        }

        private void btntotal_Click(object sender, EventArgs e)
        {
            SatisSipari siparis = new SatisSipari();
            siparis.PersonelID = Convert.ToInt32(this.Tag);
            siparis.SatisTarih = (DateTime.Now.Date.ToString());
            var soru = (from x in db.Personels where x.PersonelID == Convert.ToInt32(this.Tag) select x).FirstOrDefault();
            siparis.SubeID = soru.SubeID;
            db.SatisSiparis.InsertOnSubmit(siparis);
            db.SubmitChanges();
            foreach (Button item in flowLayoutPanel1.Controls)
            {
                SatisSiparisDetay siparisdetay = new SatisSiparisDetay();
                string[] bilgi = item.Tag.ToString().Split('/');
                var sorgu = (from x in db.Uruns where x.UrunBarkod == bilgi[0] select x).FirstOrDefault();
                siparisdetay.BirimFiyat = sorgu.UrunFiyat;
                siparisdetay.Miktar = int.Parse(bilgi[1]);
                siparisdetay.SatisSiparisID = siparis.SatisSiparisID;
                siparisdetay.UrunID = sorgu.UrunID;
                db.SatisSiparisDetays.InsertOnSubmit(siparisdetay);
                db.SubmitChanges();
            }
            flowLayoutPanel1.Controls.Clear();
            listreset();
            timer1.Start();
        }
        private void Primhesapla()
        {
            lblpersonel.Text = this.Text;
            int total = 0;
            var soru = from x in db.SatisSiparis where x.SatisTarih == DateTime.Now.Date.ToString() where x.PersonelID == Convert.ToInt32(this.Tag) select x;
            foreach (var item in soru)
            {
                var sorgu = from x in db.SatisSiparisDetays where x.SatisSiparisID == item.SatisSiparisID select x;
                foreach (var detay in sorgu)
                {
                    total += detay.Miktar * int.Parse(detay.BirimFiyat);
                }
            }
            lbltotal.Text = total.ToString() + " TL";
            lblprim.Text = Convert.ToDouble(total * 0.05).ToString() + " TL";
        }

        private void Form2_TextChanged(object sender, EventArgs e)
        {
            Primhesapla();
        }
        private void button2_Click(object sender, EventArgs e)
        {
            timer1.Start();
        }
        bool acik = false;
        private void timer1_Tick(object sender, EventArgs e)
        {
            gburunbilgileri.Visible = acik;
            if (acik)
            {
                gbSiparisDetay.Left += 20;

            }
            else
            {
                gbSiparisDetay.Left -= 20;

            }
            if (gbSiparisDetay.Left < 10)
            {
                timer1.Stop();
                acik = true;
                button2.Text = ">>>>>>>";
            }
            if (gbSiparisDetay.Left > 488)
            {
                timer1.Stop();
                acik = false;
                button2.Text = "<<<<<<<";
            }
        }

        private void btnsiparisler_Click(object sender, EventArgs e)
        {
            var sorgu = from x in db.SatisSiparis where x.PersonelID == Convert.ToInt32(this.Tag) select x;
            foreach (var item in sorgu)
            {
                Button btn = new Button();
                btn.Width = flowLayoutPanel1.Width - 5;
                btn.Height = 50;
                btn.Tag = item.SatisSiparisID;
                var soru = from s in db.SatisSiparisDetays where s.SatisSiparisID == item.SatisSiparisID select new { s.BirimFiyat, s.Miktar };
                int total = 0;
                foreach (var dty in soru)
                {

                    total += int.Parse(dty.BirimFiyat) * dty.Miktar;

                }
                string bilgi = "Sipariş No: " + item.SatisSiparisID.ToString() + "\nToplam Fiyat: " + total.ToString();
                btn.Text = bilgi;
                flowLayoutPanel1.Controls.Add(btn);
            }
            timer1.Start();
        }

    }
}
